require 'java'
class Element


	def initialize(elm, is_out=false)
		@elm = elm
		@is_out = is_out
	end
	
	def name
		return @elm.name
	end
	
	def value
		return @elm.value
	end
	
	def get_or_create_child(node_name)
    	java_elm = @elm.getChildByName(node_name)
		
		if java_elm == nil && @is_out
			java_elm = com.googlecode.wmbruby.MbElement.new(node_name)
			@elm.addChild(java_elm)
		end
		return java_elm
	end	
	
	def method_missing(methodname, *args)
    	puts "called: #{methodname}(#{args})"
    
    	methodname = methodname.to_s
    	
    	if methodname =~ /\=$/ then
    		# setter
    		
    		if args[0].kind_of? Element
    			valueToSet = args[0].value
    		else
    			valueToSet = args[0]
    		end
    		
    		puts "setting value on #{name} to #{valueToSet})"
    		
    		java_elm = get_or_create_child(methodname.chomp("="))
    		
			java_elm.setValue(valueToSet)
    	else
    		return Element.new(get_or_create_child(methodname), @is_out)
    	end
  	end
end
